package com.demo.controller.aircraft;

import com.demo.common.constant.WebConstants;
import com.demo.common.model.Aircraft;
import com.demo.common.model.Airroute;
import com.demo.common.model.User;
import com.demo.common.util.WebUtil;
import com.demo.controller.user.UserChangePwdValidator;
import com.demo.controller.user.UserValidator;
import com.demo.interceptor.WebInterceptor;
import com.demo.service.AircraftService;
import com.demo.service.AirrouteService;
import com.demo.service.UserService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;

/**
 * 飞机机型管理
 */
@Before(WebInterceptor.class)
public class AircraftController extends Controller {

    @Inject
    AircraftService aircraftService;

    @Inject
    AirrouteService airrouteService;

    public void index() {
        Kv cond=Kv.create();
        if(StrKit.notBlank(getPara("airno"))){
            cond.set("a.airno",getPara("airno"));
        }
        if(StrKit.notBlank(getPara("airtype"))){
            cond.set("a.airtype",getPara("airtype"));
        }
        if(StrKit.notBlank(getPara("location"))){
            cond.set("a.location",getPara("location"));
        }
        setAttr("aircraftPage", aircraftService.paginate(getParaToInt(0, 1), 10,Kv.by("cond",cond)));
        render("aircraft.html");
    }

    public void add() {
    }

    /**
     * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
     * 并要对数据进正确性进行验证，在此仅为了偷懒
     */
    @Before(AircraftValidator.class)
    public void save() {
        getBean(Aircraft.class).save();
        redirect("/aircraft");
    }

    public void edit() {
        setAttr("aircraft", aircraftService.findById(getParaToInt()));
    }

    /**
     * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
     * 并要对数据进正确性进行验证，在此仅为了偷懒
     */
    @Before(AircraftValidator.class)
    public void update() {
        getBean(Aircraft.class).update();
        redirect("/aircraft");
    }


    public void delete() {
        aircraftService.deleteById(getParaToInt());
        redirect("/aircraft");
    }

    public void deleteairroute() {
        Aircraft aircraft = aircraftService.findById(getParaToInt());
        aircraft.setAirrouteid(0);
        aircraft.update();
        redirect("/aircraft");
    }


    public void airroute(){
        setAttr("aircraft", aircraftService.findById(getParaToInt()));
        setAttr("routes",airrouteService.findAll());
    }

    @Before(AircraftrouteValiator.class)
    public void aircraftupdate(){
        getBean(Aircraft.class).update();
        redirect("/aircraft");
    }


}
